#include "U2Exception.h"

#include "U2StringUtil.h"
#include "U2LogManager.h"


U2EG_NAMESPACE_USING


U2Exception::U2Exception(int num, const U2String& desc, const U2String& src)
: line(0)
, number(num)
, description(desc)
, source(src)
{
    // Log this error - not any more, allow catchers to do it
    //U2LogManager::getSingleton().logMessage(this->getFullDescription());
}

U2Exception::U2Exception(int num, const U2String& desc, const U2String& src, 
                     const char* typ, const char* fil, long lin)
: line(lin)
, number(num)
, typeName(typ)
, description(desc)
, source(src)
, file(fil)
{
    // Log this error, mask it from debug though since it may be caught and ignored
    if(U2LogManager::getSingletonPtr())
    {
        U2LogManager::getSingleton().logMessage(
            this->getFullDescription(), 
            LML_CRITICAL, true);
    }
}

U2Exception::U2Exception(const U2Exception& rhs)
: line(rhs.line)
, number(rhs.number)
, typeName(rhs.typeName)
, description(rhs.description)
, source(rhs.source)
, file(rhs.file)
{
}

void U2Exception::operator = ( const U2Exception& rhs )
{
    description = rhs.description;
    number = rhs.number;
    source = rhs.source;
    file = rhs.file;
    line = rhs.line;
    typeName = rhs.typeName;
}

const U2String& U2Exception::getFullDescription(void) const
{
    if (fullDesc.empty())
    {
        U2StringUtil::U2StrStreamType desc;

        desc <<  "U2 EXCEPTION(" << number << ":" << typeName << "): "
            << description 
            << " in " << source;

        if( line > 0 )
        {
            desc << " at " << file << " (line " << line << ")";
        }

        fullDesc = desc.str();
    }

    return fullDesc;
}

int U2Exception::getNumber(void) const throw()
{
    return number;
}